var private const MIN_ZOOM_SIZE = -10;
var private const MAX_ZOOM_SIZE = 200;
//
//	現在のカーソルを画面中央に
//
function DispCenter()
{
	var	temp_vec, temp;
	var	win = WIN_PERS;

	temp = Position(Cursor())*pers_mat;
	temp_vec = temp-vector(abs(temp),0,0);
	pers_mat = pers_mat*move(UNIT_MATRIX, -temp_vec);
	SetPers( pers_mat );
	if( Cursor() != Center())
	{
		Center( Cursor());
		win = win | WIN_XY | WIN_YZ | WIN_ZX;
	}
	Update( CLEAR, win );
}

//
//	フィット
//
function Fit()
{
	var	prev_sel, temp_mat;
	var	pos[2], range[3], size, len;
	var	temp, width, height, win_type;

	prev_sel = Select();
	if(( ShiftStat() & 1 ) != 1 )
		SelectAll( TRUE );
	if( SelectPolygons() == 0 )
	{
		Warning();
		if( novice_flag == ON )
			Message( "面がないので表示範囲を設定できません" );
		return;
	}
	else
	{
		SelectBox( pos );
		Center( (pos[0]+pos[1])*scale(UNIT_MATRIX, 0.5, 0.5, 0.5 ));
	}

	len = abs(Position(pos[1])-Position(pos[0]))/2;
	width = ViewWidth( WIN_PERS );
	height = ViewWidth( WIN_PERS );
	if( width > height )
		len = 1.5*(0.0+width)*len/(height*tan((0.0+pers_deg/2)*PI/180));
	else
		len = 1.5*len/tan((0.0+pers_deg/2)*PI/180 );
	temp = Position(Center())*pers_mat-vector(len,0,0);
	pers_mat = pers_mat*move(UNIT_MATRIX, -temp);

	win_type = Window();
	if( win_type == WIN_ALL )
		win_type = WIN_XY;
	width = ViewWidth( win_type );
	height = ViewHeight( win_type );
	if( width > height )
		len = height;
	else
		len = width;

	element(Position(pos[1])-Position(pos[0]), range );
	if( range[0] > range[1] )
	{
		if( range[0] > range[2] )
			size = range[0];
		else
			size = range[2];
	}
	else
	{
		if( range[1] > range[2] )
			size = range[1];
		else
			size = range[2];
	}
	size = size*3/2;
	if( zoomsize > 0 )
		temp = (0.0+zoomsize)*size/len;
	else
		temp = size/((0.0-zoomsize)*len);
	if( temp < 1 )
		temp = (-1.0)/temp;
	zoomsize = 0+temp;
	if( zoomsize > MAX_ZOOM_SIZE )
	{
		zoomsize = MAX_ZOOM_SIZE;
		ButtonEnable( zoomdown_id, BUTTON_DISABLE );
		UpdateButton( zoomdown_id );
	}
	if( zoomsize < MIN_ZOOM_SIZE )
		zoomsize = MIN_ZOOM_SIZE;
	if( zoomsize == 0 )
		zoomsize = 1;
	if( zoomsize > 1 )
	{
		ButtonEnable( zoomup_id, BUTTON_ENABLE );
		UpdateButton( zoomup_id );
	}
	else
	{
		ButtonEnable( zoomup_id, BUTTON_DISABLE );
		UpdateButton( zoomup_id );
	}
	if( zoomsize < MAX_ZOOM_SIZE )
	{
		ButtonEnable( zoomdown_id, BUTTON_ENABLE );
		UpdateButton( zoomdown_id );
	}
	SetZoom( -zoomsize );
	StatusZoom();
	Select( prev_sel );
	SetPers( pers_mat );
	Update( CLEAR , WIN_XY | WIN_YZ | WIN_ZX );
}

//
//	表示範囲指定
//
function SelectZoom()
{
	var width_size, height_size;
	var cur0, cur1;
	var key, pos_temp[3], pos0[3], pos1[3], size, temp, len;
	var draw_flag = ON, pick;
	var ver, i, j;

	PushMenu();
	MenuPosition( Menu( " 表示範囲指定中",
			"指定中止	ESC", MenuQuit ), Menu_Title );
	quit_flag = FALSE;

	ver = 0;
	temp = 0;
	cur = Cursor();
	cur0 = cur;
	pick = cur;
	input_flag = OFF;
	while( TRUE )
	{
		if( draw_flag == ON )
		{
			if( ver != 0 )
				DrawBox( cur0, cur );
		}
		key = WaitEvent();
		if( key )
			input_flag = ON;
		cur = Cursor();
		key = KeyCode();
		if( input_flag | key == INPUT_KEY )
		{
			if( ver == 0 )
			{
				cur0 = cur;
				ver++;
			}
			else
			{
				cur1 = cur;
				break;
			}
		}
		else if( key == ESC | quit_flag == TRUE )
		{
			PopandClear( OVERWRITE );
			return;
		}
		else if( key == BS )
		{
			if( ver == 0 )
			{
				PopandClear( OVERWRITE );
				return;
			}
			else
			{
				ver--;
				DrawBox( pick, cur0 );
			}
		}
		else
			temp = KeyProcess( key );
		if( ((key!=0 ) & (key != INPUT_KEY)) | ( pick != Cursor()) )
		{
			draw_flag = ON;
			if( ver != 0 )
				DrawBox( cur0, pick );
		}
		else
			draw_flag = OFF;
		pick = Cursor();
		input_flag = OFF;
	}
	Clear( OVERWRITE );

	if( cur0 == cur1 )
		Center( cur0 );
	else
	{
		temp = MouseWindow();
		if( temp == WIN_PERS )
			temp = WIN_XY;
		width_size = ViewWidth( temp );
		height_size = ViewHeight( temp );
		Position( cur0, pos0 );
		Position( cur1, pos1 );
		Center(( cur0 + cur1 )*scale(UNIT_MATRIX, 0.5, 0.5, 0.5 ));
		pos0[0] = 0.0+abs( pos0[0] - pos1[0] );
		pos0[1] = 0.0+abs( pos0[1] - pos1[1] );
		pos0[2] = 0.0+abs( pos0[2] - pos1[2] );
		if( temp == WIN_XY )
		{
			i = 0;
			j = 1;
		}
		else if( temp == WIN_YZ )
		{
			i = 1;
			j = 2;
		}
		else
		{
			i = 0;
			j = 2;
		}
		if( pos0[i] == 0 & pos0[j] == 0 )
		{
			Update( CLEAR, WIN_XY | WIN_YZ | WIN_ZX );
			PopandClear( OVERWRITE );
			return;
		}
		if( pos0[i] * height_size / width_size > pos0[j] )
		{
			size = pos0[i];
			len = width_size;
		}
		else
		{
			size = pos0[j];
			len = height_size;
		}
		if( ShiftStat() & 1 )
		{
			if( zoomsize > 0 )
				temp = (0.0+zoomsize)*len/size;
			else
				temp = ((0.0+len)/(size))/(-zoomsize);
			if( temp < 1 )
			{
				temp = (-1.0)/temp;
				if( temp < MIN_ZOOM_SIZE )
					temp = MIN_ZOOM_SIZE;
			}
		}
		else
		{
			if( zoomsize > 0 )
				temp = (0.0+zoomsize) * size / len;
			else
				temp = size/((0.0-zoomsize)*len);
			if( temp < 1 )
				temp = (-1.0)/temp;
		}
		zoomsize = 0+temp;
	}
	if( zoomsize > MAX_ZOOM_SIZE )
	{
		zoomsize = MAX_ZOOM_SIZE;
		ButtonEnable( zoomdown_id, BUTTON_DISABLE );
		UpdateButton( zoomdown_id );
	}
	if( zoomsize < MIN_ZOOM_SIZE )
		zoomsize = MIN_ZOOM_SIZE;
	if( zoomsize == 0 )
		zoomsize = 1;
	if( zoomsize > 1 )
	{
		ButtonEnable( zoomup_id, BUTTON_ENABLE );
		UpdateButton( zoomup_id );
	}
	else
	{
		ButtonEnable( zoomup_id, BUTTON_DISABLE );
		UpdateButton( zoomup_id );
	}
	if( zoomsize < MAX_ZOOM_SIZE )
	{
		ButtonEnable( zoomdown_id, BUTTON_ENABLE );
		UpdateButton( zoomdown_id );
	}
	SetZoom( -zoomsize );
	StatusZoom();
	Update( CLEAR, WIN_XY | WIN_YZ | WIN_ZX );
	PopMenu();
}

function WindowChange()
{
	var	win;
	var	num;
	var	msg0[5];

	win = Window();
	if( win == WIN_ALL )
		num = 0;
	else if( win == WIN_XY )
		num = 1;
	else if( win == WIN_YZ )
		num = 2;
	else if( win == WIN_ZX )
		num = 3;
	else
		num = 4;

	DlogOpen( " 表示図面変更", 1 );
	msg0[0] = " 四面図 ";
	msg0[1] = "ＸＹ図面";
	msg0[2] = "ＹＺ図面";
	msg0[3] = "ＺＸ図面";
	msg0[4] = " 透視図 ";
	DlogSelect( 0, "表示図面", msg0, num );
	if( DlogWait() )
	{
		win = DlogAnswer( 0 );
		if( win == 0 )
			Window( WIN_ALL );
		else if( win == 1 )
			Window( WIN_XY );
		else if( win == 2 )
			Window( WIN_YZ );
		else if( win == 3 )
			Window( WIN_ZX );
		else
			Window( WIN_PERS );
	}
}

function Invisible()
{
	var	temp;
	if ( SelectPolygons() > 0 )
	{
		temp = Select();
		SelectAll( TRUE, SELECT_XOR );
		if( SelectPolygons() == 0 )
		{
			Select( temp );
			Warning();
			if( novice_flag == ON )
				Message( "非表示にする面が存在しません" );
		}
		else
		{
			Select( temp );
			invisible_flag = TRUE ;
			PolyInvisible( TRUE );
			Update( TRUE );
			prevnext_flag = FALSE;
			pers_rotation_flag = FALSE;
		}
	}
	else
	{
		Warning();
		if( novice_flag == ON )
		{
			SelectAll( TRUE );
			if( SelectPolygons() != 0 )
				Message( "すべての面を非表示にすることはできません" );
			else
				Message( "非表示にする面が存在しません" );
			SelectAll( FALSE );
		}
	}
}

function Visible()
{
	if( invisible_flag == TRUE )
	{
		invisible_flag = FALSE ;
		PolyInvisible( FALSE );
		Update( TRUE );
		pers_rotation_flag = FALSE;
		prevnext_flag = FALSE;
	}
	else
	{
		Warning();
		if( novice_flag == ON )
			Message( "非表示されている面は存在しません" );
	}
}

function ZoomDown()
{
	var	i;

	for( i = 0;i<MAX_ZOOM_NUM;i++ )
	{
		if( zoom_number[i] > zoomsize )
			break;
		else if( zoom_number[i] == zoomsize )
		{
			i++;
			break;
		}
	}
	if( i >= MAX_ZOOM_NUM )
	{
		i = MAX_ZOOM_NUM - 1;
		Warning();
	}
	if( i == ( MAX_ZOOM_NUM - 1 ))
	{
		ButtonEnable( zoomdown_id, BUTTON_DISABLE );
		UpdateButton( zoomdown_id );
	}
	else if( i == 1 )
	{
		ButtonEnable( zoomup_id, BUTTON_ENABLE );
		UpdateButton( zoomup_id );
	}
	zoomsize = zoom_number[i];
	SetZoom( -zoomsize );
	StatusZoom();
	Update( CLEAR, WIN_XY | WIN_YZ | WIN_ZX );
}

function ZoomUp()
{
	var	i;

	for( i = 0;i<MAX_ZOOM_NUM;i++ )
	{
		if( zoom_number[i] >= zoomsize )
			break;
	}
	i--;
	if( i < 0 )
	{
		i = 0;
		Warning();
	}
	if( i == 0 )
	{
		ButtonEnable( zoomup_id, BUTTON_DISABLE );
		UpdateButton( zoomup_id );
	}
	else if( i == (MAX_ZOOM_NUM -2 ))
	{
		ButtonEnable( zoomdown_id, BUTTON_ENABLE );
		UpdateButton( zoomdown_id );
	}
	zoomsize = zoom_number[i];
	SetZoom( -zoomsize );
	StatusZoom();
	Update( CLEAR, WIN_XY | WIN_YZ | WIN_ZX );
}

function InvisibleDisp()
{
	if( DrawInvisible() == FALSE )
	{
		DrawInvisible( TRUE );
		Update( OVERWRITE );
	}
	else
	{
		DrawInvisible( FALSE );
		Update( CLEAR );
	}
//	if( InvisibleFlag == TRUE )
//		Update( CLEAR );
}

function MeshDisp()
{
	if( meshdisp_flag == ON )
	{
		meshdisp_flag = OFF;
		SetGrid( 0, mouse_grid );
	}
	else
	{
		meshdisp_flag = ON;
		SetGrid( mesh_grid, mouse_grid );
	}
	Update( CLEAR, WIN_XY | WIN_YZ | WIN_ZX );
}

function HousenDisp()
{
	if( housendisp_flag == ON )
	{
		housendisp_flag = OFF;
		VisibleVector( 0 );
	}
	else
	{
		housendisp_flag = ON;
		VisibleVector( vecvisible_len );
	}
	Update( CLEAR, WIN_PERS );
}

function TenDisp()
{
	if( tendisp_flag == ON )
	{
		tendisp_flag = OFF;
		VisibleVertex( 0 );
	}
	else
	{
		tendisp_flag = ON;
		VisibleVertex( 1 );
	}
	Update( CLEAR );
}

function private Check_flag()
{
	var	temp;

	temp = MenuFunction();
	if( temp == MeshDisp )
	{
		if( meshdisp_flag == ON )
			MenuEnable( MENU_CHECK );
		else
			MenuEnable( MENU_NOCHECK );
	}
	else if( temp == HousenDisp )
	{
		if( housendisp_flag == ON )
			MenuEnable( MENU_CHECK );
		else
			MenuEnable( MENU_NOCHECK );
	}
	else if( temp == TenDisp )
	{
		if( tendisp_flag == ON )
			MenuEnable( MENU_CHECK );
		else
			MenuEnable( MENU_NOCHECK );
	}
}

function private InvisibleCheck()
{
	var	sel, temp;

	if( SelectPolygons() == 0 )
		temp = MENU_DISABLE;
	else
	{
		sel = Select();
		SelectAll( TRUE, SELECT_XOR );
		if( SelectPolygons() != 0 )
			temp = MENU_ENABLE;
		else
			temp = MENU_DISABLE;
		Select( sel );
	}
	MenuEnable( temp );
}

function private VisibleCheck()
{
	if( invisible_flag == OFF )
		MenuEnable( MENU_DISABLE );
	else
		MenuEnable( MENU_ENABLE );
}

function private InvisibleDispCheck()
{
	if( DrawInvisible() == FALSE )
		MenuEnable( MENU_NOCHECK );
	else
		MenuEnable( MENU_CHECK );
}

function MeshSetup()
{
	var dispgrid;

	dispgrid = sliderconv2( mesh_grid );
	DlogOpen( "メッシュ幅設定", 1 );
	DlogSlider( 0, "メッシュ幅", SLIDER_HABA, MAX_SLIDER_NUM2, dispgrid, gridconv2 );
	if ( DlogWait() )
	{
		dispgrid = DlogAnswer( 0 );
		mesh_grid = gridconv2( dispgrid );
		if( meshdisp_flag == ON )
		{
			SetGrid( mesh_grid, mouse_grid );
			Update( CLEAR, WIN_XY | WIN_YZ | WIN_ZX );
		}
		StatusMesh();
	}
}

function HousenSetup()
{
	var dispvec;

	dispvec = sliderconv1( vecvisible_len );
	DlogOpen( "法線の長さ設定", 1 );
	DlogSlider( 0, "法線の長さ", SLIDER_HABA, MAX_SLIDER_NUM1, dispvec, gridconv1 );
	if ( DlogWait() )
	{
		dispvec = DlogAnswer( 0 );
		vecvisible_len = gridconv1( dispvec );
		if( housendisp_flag == ON )
		{
			VisibleVector( vecvisible_len );
			Update( CLEAR, WIN_PERS );
		}
	}
}

function PersInit()
{
	DefaultPers();
	Update( CLEAR, WIN_PERS );
}

if( MACHINE == "WINDOWS" )
{
	MenuPosition(
		Menu( " 表示(&D)",
			"作画               ",		Rend,PolyCheck,
			"作画設定...        ",		RendSetup,
			"FXGA表示           ",		Fxga,PolyCheck,
			"-------------------",		SEPARATE_MENU,
			"選択面のみ表示     	I",	Invisible,InvisibleCheck,
			"全ての面を表示     	ｼﾌﾄ+I",	Visible,VisibleCheck,
			"非表示面を薄く表示 ",		InvisibleDisp,InvisibleDispCheck,
			"-------------------",		SEPARATE_MENU,
			"表示範囲指定       	Z",	SelectZoom,
			"表示範囲拡大       ",		ZoomDown,
			"表示範囲縮小       ",		ZoomUp,
			"-------------------",		SEPARATE_MENU,
			"表示図面変更...    ",		WindowChange,
			"-------------------",		SEPARATE_MENU,
			"点を表示           ",		TenDisp,Check_flag,
			"-------------------",		SEPARATE_MENU,
			"ﾒｯｼｭを表示         ",		MeshDisp,Check_flag,
			"ﾒｯｼｭ幅設定...      ",		MeshSetup,
			"-------------------",		SEPARATE_MENU,
			"法線を表示         ",		HousenDisp,Check_flag,
			"法線表示長さ設定...",		HousenSetup,
			"-------------------",		SEPARATE_MENU,
			"透視図初期化       	HOME",	PersInit,
			"表示初期化         	ｼﾌﾄ+HOME",		AllInit
		), Menu_disp
	);
}
else
{
	MenuPosition(
		Menu( " 表示(&D)",
			"作画               ",		Rend,PolyCheck,
			"作画設定...        ",		RendSetup,
			"-------------------",		SEPARATE_MENU,
			"選択面のみ表示     	I",	Invisible,InvisibleCheck,
			"全ての面を表示     	ｼﾌﾄ+I",	Visible,VisibleCheck,
			"非表示面を薄く表示 ",		InvisibleDisp,InvisibleDispCheck,
			"-------------------",		SEPARATE_MENU,
			"表示範囲指定       	Z",	SelectZoom,
			"表示範囲拡大       ",		ZoomDown,
			"表示範囲縮小       ",		ZoomUp,
			"-------------------",		SEPARATE_MENU,
			"表示図面変更...    ",		WindowChange,
			"-------------------",		SEPARATE_MENU,
			"点を表示           ",		TenDisp,Check_flag,
			"-------------------",		SEPARATE_MENU,
			"ﾒｯｼｭを表示         ",		MeshDisp,Check_flag,
			"ﾒｯｼｭ幅設定...      ",		MeshSetup,
			"-------------------",		SEPARATE_MENU,
			"法線を表示         ",		HousenDisp,Check_flag,
			"法線表示長さ設定...",		HousenSetup,
			"-------------------",		SEPARATE_MENU,
			"透視図初期化       	HOME",	PersInit,
			"表示初期化         	ｼﾌﾄ+HOME",		AllInit
		), Menu_disp
	);
}

